﻿using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Windows.Forms;
using myWinForm.Properties;

namespace myWinForm
{
    public partial class myWinForm : Form
    {
        public myWinForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();  //显示选择文件对话框  
            openFileDialog1.InitialDirectory = @"E:\Proj\zss2\skin.1yyg.net\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //所有的文件格式  
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog1.FileName;   //显示文件路径  
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string filePath = textBox1.Text;

            string str = GetFileHash(filePath);

            Hash hash = new Hash();

            DateTime t0 = DateTime.Now;
            for (int i = 0; i < 10000; i++)
            {
                hash.SHA1File(filePath);
            }
            DateTime t1 = DateTime.Now;
            var a = t1 - t0;
            richTextBox1.Text = "span = " + a.TotalSeconds;

            DateTime t3 = DateTime.Now;
            for (int i = 0; i < 10000; i++)
            {
                hash.MD5File(filePath);
            }
            DateTime t4 = DateTime.Now;
            var a2 = t4 - t3;
            richTextBox1.Text += "\nspan = " + a2.TotalSeconds;

            DateTime t5 = DateTime.Now;
            for (int i = 0; i < 10000; i++)
            {
                GetFileHash(filePath);
            }
            DateTime t6 = DateTime.Now;
            var a3 = t6 - t5;
            richTextBox1.Text += "\nspan = " + a3.TotalSeconds;



            this.textBox2.Text = str;   //显示文件路径  
        }

        /// <summary>
        /// 计算MD5
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string GetFileHash(string filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hash_byte = md5.ComputeHash(file);
            string str = System.BitConverter.ToString(hash_byte);
            str = str.Replace("-", "");
            return str;
        }

        private static int fileNum = 0;

        private long fileCapacity = 0;

        private void CalcFolder(string path)
        {

            var files = Directory.GetFiles(path);
            var dics = Directory.GetDirectories(path);

            for (int i = 0; i < files.Length; i++)
            {
                var extend = Path.GetExtension(files[i]);
                if ((extend == ".js" || extend == ".css") && files[i].IndexOf("source") == -1)
                {
                    FileInfo fileInfo = new FileInfo(files[i]);
                    fileCapacity += fileInfo.Length;
                    fileNum++;

                    richTextBox1.Text += "\n   " + fileNum + "    " + Math.Ceiling((decimal)fileInfo.Length / 1024) + "      " + files[i];


                }
            }

            for (int i = 0; i < dics.Length; i++)
            {
                CalcFolder(dics[i]);
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            FolderBrowserDialog browserDialog = new FolderBrowserDialog();
            browserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            if (browserDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = browserDialog.SelectedPath;   //显示文件路径  
                CalcFolder(browserDialog.SelectedPath);

                richTextBox1.Text += "\n\n\n\n" + "文件数    " + fileNum + "   总大小   " + Math.Ceiling((decimal)fileCapacity / 1024) + "KB";
                richTextBox1.Text += "每个js css文件大小为：" + Math.Ceiling((decimal)fileCapacity / 1024 / fileNum) + "KB";
            }
        }



        private void myWinForm_Load(object sender, EventArgs e)
        {
            var addresses = GetLocalAddress();
            cmbIP.Items.Clear();
            if (addresses.Length > 0)
            {
                foreach (string t in addresses)
                {
                    cmbIP.Items.Add(t);
                }
                cmbIP.Text = (string)cmbIP.Items[2];
            }
        }

        public string[] GetLocalAddress()
        {
            // 获取主机名
            var strHostName = Dns.GetHostName();
            // 根据主机名进行查找
            var iphostentry = Dns.GetHostEntry(strHostName);
            var retval = new string[iphostentry.AddressList.Length];
            var i = 0;
            foreach (var ipaddress in iphostentry.AddressList)
            {
                retval[i] = ipaddress.ToString();
                i++;
            }
            return retval;
        }

        private void comIP_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
